import time

from selenium.webdriver.common.by import By

from page_object.base_page import BasePage
from utils.logger import logger
from utils.tool import handle_text


class CommodityInfoPage(BasePage):
    _COMMODITY_TITLE = (By.CSS_SELECTOR, '.sku-name')
    _ADD_COMMODITY = (By.CSS_SELECTOR, '.btn-add')
    _DELETE_COMMODITY = (By.CSS_SELECTOR, '.btn-reduce')
    _SEND_COMMODITY = (By.CSS_SELECTOR, '.buy-num')
    _SEND_COMMODITY_CITY = (By.CSS_SELECTOR, '.ui-area-text')
    _SEND_COMMODITY_CITY1 = (By.CSS_SELECTOR, '.ui-area-tab>.ui-switchable-item')
    _SELECT_CITY = (By.CSS_SELECTOR, '.ui-switchable-panel-selected li')
    _COMMODITY_NUM = (By.CSS_SELECTOR, '.buy-num')
    _COMMODITY_PRICE = (By.CSS_SELECTOR, '.p-price>.price')
    _TEXT = (By.CSS_SELECTOR, '#summary-tips')
    _APPRAISE = (By.CSS_SELECTOR, 'a.count')
    _FAVORABLE_RATE = (By.CSS_SELECTOR, '.percent-con')
    _LOOK_BEFORE_RATE = (By.CSS_SELECTOR, '.comm-curr-sku')
    _NEGATIVE_COMMENT = (By.XPATH, '//*[text()="差评"]/em')
    _ALL_COMMENT = (By.XPATH, '//*[text()="全部评价"]/em')

    def get_commodity_title(self):
        """获取商品详情的title"""
        return self.find(self._COMMODITY_TITLE).get_attribute('innerText')

    def add_commodity(self, num=1):
        """添加商品"""
        for i in range(num):
            self.find_and_click(self._ADD_COMMODITY)
        return self

    def delete_commodity(self, num=1):
        """删除商品"""
        time.sleep(1)
        for i in range(num):
            self.find_and_click(self._DELETE_COMMODITY)
        return self

    def send_commodity_num(self, num=1):
        """添加商品"""
        self.clear_and_send(num, self._SEND_COMMODITY)
        return self

    def get_commodity_addr(self, city_name):
        """选择地区"""
        self.find_and_click(self._SEND_COMMODITY_CITY)
        self.find_and_click(self._SEND_COMMODITY_CITY1)
        _COMMODITY_CITY = (By.XPATH, f'//*[@class="ui-area-content"]//a[text()="{city_name}"]')
        self.find_and_click(_COMMODITY_CITY)
        city_ele_list = self.finds(self._SELECT_CITY)
        city_list = []
        for city in city_ele_list:
            city_list.append(city.text)
        return city_list

    def get_commodity_num(self):
        """获取当前商品数量"""
        return self.find(self._COMMODITY_NUM).get_attribute('value')

    def get_commodity_price(self):
        """获取当前商品数量"""
        return self.find(self._COMMODITY_PRICE).get_attribute('innerText')

    def click_text(self):
        """点击文字"""
        time.sleep(0.5)
        self.find_and_click(self._TEXT)
        return self

    def click_appraise(self):
        """点击评价"""
        self.find_and_click(self._APPRAISE)
        return self

    def get_comment_num(self):
        """获取评价详情"""
        # self.find_and_click(self._LOOK_BEFORE_RATE)
        favorable_rate = int(self.find(self._FAVORABLE_RATE).get_attribute('innerText')[:-1])
        negative_comment = handle_text(self.find(self._NEGATIVE_COMMENT).get_attribute('innerText'))
        all_comment = handle_text(self.find(self._ALL_COMMENT).get_attribute('innerText'))
        return {"好评率": favorable_rate, "差评": negative_comment, "全部评价": all_comment}

